<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class gs extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->model('task_mod','tk');
		$this->load->model('gs_mod','gs');
		$this->load->model('Track_mod','T');
		$this->load->helper('date');
	}
										
	function index()
	{
		$this->checklogining();
		
		$data['uid']=$this->session->userdata('u_id');
		$user_list=$this->tk->getalluser();
		$data["user_list"]=$user_list;    
		$data['gslist'] = $this->gs->getgsList();
		$this->load->view('contact',$data);
	}
	
	function addgs()
	{
		$this->checklogining();
		
		$num = $this->input->post('num',TRUE);
		
		for($i=1;$i<=$num;$i++):
		$data['name'] = $this->input->post('gsname'.$i,TRUE);
		
		if(!($this->gs->Repeatcheck('name',$data['name'])))
		{
			continue;
		}
		
		$data['website'] = $this->input->post('gswebsite'.$i,TRUE);
		$data['address'] = $this->input->post('gsaddress'.$i,TRUE);
		
		
		$this->gs->Insertgs($data);
		endfor;
		redirect('gs/');
	}
	
	function showcustomer($gsid)
	{
		$this->checklogining();
		
		$data['uid'] = $this->session->userdata('u_id');
		$user_list = $this->tk->getalluser();
		$data["user_list"] = $user_list;

		$data['gsinfo'] = $this->gs->getgsinfo($gsid);
		$data['customerlist'] = $this->gs->customer->getCustomerListBygsid($gsid);
		$data['gsid'] = $gsid;
		
		$data['trackList'] = $this->T->getgsTrackBygsid($gsid);
		
		$this->load->view('contact_detail' ,$data);
	}
	
	function showcustomerindex()
	{
		$data['uid'] = $this->session->userdata('u_id');
		$user_list = $this->tk->getalluser();
		$data["user_list"] = $user_list;

		$this->load->view('addContact' ,$data);
	}
	
	function showAddgs($num = '')
	{
		$i = 1;

		$data['i'] = $i;
		$data['num'] = empty($num)? 1:$num;
		
		$this->load->view('Addgs',$data);
	}
	
	function auto_wanc()
	{
		$term = $this->input->get('term');
		die(json_encode($this->gs->getgsListallTag(urldecode($term))));
	}
	
	function auto_gs($v)
	{
		
		$arr = $this->gs->searchLink(urldecode($v));
		
		if(!empty($arr))
		{
			foreach ($arr AS $k =>$a)
			{
				$p[$k]['gsid']		= $a['id'];
				$p[$k]['gsname']	= $a['name'];
				$p[$k]['gssite']	= $a['website'];
				$p[$k]['gsaddress']	= $a['address'];
			}
		}else{
			$p = 'false';
		}
		die(json_encode($p));
	}
	function addcustomer()
	{
		$this->checklogining();
		
		
		$data['name'] = $this->input->post('na').$this->input->post('me');
		$data['Mobile'] = $this->input->post('telephone');
		$data['email'] = $this->input->post('email');
		$data['qq'] =  $this->input->post('qq_tle');
		$data['msn'] = $this->input->post('msn');
		$data['marks'] = '';
		
		if($this->gs->checkgsRepeat($this->input->post('gsname')))
		{
			$gss['name'] = $this->input->post('gsname');
			$gss['website'] = $this->input->post('website');
			$gss['address'] = $this->input->post('address');
			
			$this->gs->Insertgs($gss);
			$data['gsid'] = $this->db->insert_id();
		}else{
			
			$data['gsid'] = $this->gs->getgsIdByName($this->input->post('gsname'));
		}
		$this->gs->customer->InsertCustomer($data);
		
		$cid = $this->db->insert_id();
		
		$ca['gsid'] = $data['gsid'];
		
		$ca['uid'] = $ba['uid'] = $this->session->userdata('u_id');
		$ca['cid'] = $cid;
		$ba['t_name'] = '客户['.$data['name'].']已进行了第一次访问';
		$ba['t_desc'] = '';
		$ba['type'] = 2;
		$ba['status'] = 1;
		$ba['is_rel'] = 0;
		$ba['t_completedtime'] = date('Y-m-d H:i:s',time());
		
		$this->gs->customer->FirstAddCustomerWithTrack($ba);
		$ca['tid'] = $this->db->insert_id();
		$ca['date'] = time();
		$ca['content'] = $ba['t_name'];
		
		$this->gs->customer->AddCustomerWithtmsTrack($ca);
		
		
		redirect('gs/showcustomer/'.$data['gsid']);
	}
	
	public function checklogining()
	{
		if(!($this->session->userdata('u_id')))
		{
			header("location:".site_url('user/index'));
		}
	
	}
}
?>